package com.cmwa.solution.business.myHome.todo.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import com.cmwa.solution.business.customerEquity.estimate.dao.EstimateInfoDao;
import com.cmwa.solution.business.customerEquity.estimate.entity.EstimateInfoVo;
import com.cmwa.solution.business.customerEquity.navinfo.dao.NavInfoDao;
import com.cmwa.solution.business.myHome.todo.entity.TodoBizRelVo;
import com.cmwa.solution.business.myHome.todo.entity.TodoVo;
import com.cmwa.solution.business.myHome.todo.enums.ServiceType;
import com.cmwa.solution.business.myHome.todo.enums.TodoBizType;
import com.cmwa.solution.business.myHome.todo.service.AbsTodoBizRelGenerator;
import com.cmwa.solution.business.myHome.todo.service.TodoBizRelGenerateFactory;
import com.cmwa.solution.utils.DateUtils;
import com.cmwa.solution.utils.SessionUtils;
import com.google.common.base.Joiner;

@Service
public class EsitmateTodoBizRelGenerate extends AbsTodoBizRelGenerator implements InitializingBean {
	
	private static final Log logger = LogFactory.getLog(EsitmateTodoBizRelGenerate.class);

	@Resource
	private EstimateInfoDao estimateInfoDao;
	
	@Resource
	private NavInfoDao navInfoDao;
	
	public EsitmateTodoBizRelGenerate() {
		this.setServiceTypes(ServiceType.CUST_EQUITY);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		TodoBizRelGenerateFactory.addGenerator(ServiceType.CUST_EQUITY, this);
	}


	@Override
	public void generateByTodo(TodoVo todoVo) {
		// 产品费用与收益类型的待办无需联动生成数据
		if (StringUtils.equals(todoVo.getTodoType(), "FEE_YIELD")) {
			return;
		}
		
		// 保存估值表业务数据
		EstimateInfoVo estimateInfo = new EstimateInfoVo();
		String empId = SessionUtils.getEmployee().getID();
		Date todoDate = todoVo.getTodoDate();
		String date = DateUtils.formatDate(todoDate, DateUtils.DATE_FORMAT_yMd_EN);
		estimateInfo.setReportTime(date);//报告日期
		estimateInfo.setPrjCode(todoVo.getPrjCode());
		estimateInfo.setCreateId(empId);
		estimateInfo.setStatus("C");
		estimateInfoDao.insertEstimateInfo(estimateInfo);
		
		saveTodoBizRel(buildRelVo(todoVo, TodoBizType.ESTIMATE, estimateInfo.getEstimateId()));
		
		//保存净值查询模块业务数据
		if(StringUtils.isNotBlank(estimateInfo.getPrjCode())) {
			
			List<String> subPrdCodeList = navInfoDao.querySubPrdCode(estimateInfo.getPrjCode());
			for(String value :subPrdCodeList){
				EstimateInfoVo vo;
				try {
					vo = (EstimateInfoVo) BeanUtils.cloneBean(estimateInfo);
					vo.setSubPrdCode(value);
					navInfoDao.insertNavInfo(vo);
					
					saveTodoBizRel(buildRelVo(todoVo, TodoBizType.ESTIMATE_NAV, vo.getEstimateId()));
				} catch (IllegalAccessException e) {
					logger.error("IllegalAccessException,",e);
				} catch (InstantiationException e) {
					logger.error("InstantiationException,",e);
				} catch (InvocationTargetException e) {
					logger.error("InvocationTargetException,",e);
				} catch (NoSuchMethodException e) {
					logger.error("NoSuchMethodException,",e);
				}
			}
			
		} else {
			logger.error("系列产品编码为空，无法确定子产品层，故无法生成净值待办");
		}
	}

	@Override
	public void deleteByTodo(TodoVo todoVo) {
		// 估值表对应数据删除
		List<TodoBizRelVo> todoBizRels = todoBizRelDao.queryByTodo(TodoBizType.ESTIMATE, todoVo.getId());
		if (CollectionUtils.isNotEmpty(todoBizRels)) {
			List<String> bizIds = new ArrayList<>();
			for (TodoBizRelVo relVo : todoBizRels) {
				if (StringUtils.isNotBlank(relVo.getBizId())) {
					bizIds.add(relVo.getBizId());
				}
			}
			
			if (CollectionUtils.isNotEmpty(bizIds)) {
				estimateInfoDao.logicDeleteEstimateInfoById(Joiner.on(",").join(bizIds));
			}
		}
		
		// 估值表对应数据删除
		List<TodoBizRelVo> todoNavBizRels = todoBizRelDao.queryByTodo(TodoBizType.ESTIMATE_NAV, todoVo.getId());
		if (CollectionUtils.isNotEmpty(todoNavBizRels)) {
			List<String> bizIds = new ArrayList<>();
			for (TodoBizRelVo relVo : todoNavBizRels) {
				if (StringUtils.isNotBlank(relVo.getBizId())) {
					bizIds.add(relVo.getBizId());
				}
			}
			
			// 删除净值表数据
			if (CollectionUtils.isNotEmpty(bizIds)) {
				navInfoDao.physicDelete(Joiner.on(",").join(bizIds));
			}
		}
	}

}
